package com.vhall.component.service.room.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vhall.component.dao.room.RoomJoinsMapper;
import com.vhall.component.entity.room.entity.RoomJoinsEntity;
import com.vhall.component.service.room.RoomCacheService;
import com.vhall.component.service.room.RoomJoinService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author xuyongzheng
 * @since 5/8/21 3:31 PM
 **/
@Slf4j
@Service
public class RoomJoinServiceImpl implements RoomJoinService {

    @Autowired
    private RoomJoinsMapper roomJoinsMapper;

    @Autowired
    private RoomCacheService roomCacheService;


    @Override
    public Boolean updateQuestionFlag(Integer joinId, Integer flag) {
        RoomJoinsEntity entity = new RoomJoinsEntity();
        entity.setJoinId(joinId);
        entity.setUpdatedAt(LocalDateTime.now());
        entity.setIsAnsweredQuestionnaire(flag);

        int effect = roomJoinsMapper.updateById(entity);
        // 若更新到数据，则重新缓存参会信息
        if (effect > 0) {
            roomCacheService.cacheRoomJoins(joinId);
        }
        return effect > 0;
    }

    @Override
    public RoomJoinsEntity getRoomJoin(String roomId, Integer accountId) {
        LambdaQueryWrapper<RoomJoinsEntity> queryWrapper= Wrappers.lambdaQuery();
        queryWrapper.eq(RoomJoinsEntity::getRoomId, roomId);
        queryWrapper.eq(RoomJoinsEntity::getAccountId, accountId);
        queryWrapper.last(" limit 1");
        return roomJoinsMapper.selectOne(queryWrapper);
    }


}
